<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div>
            <span>姓名:</span>
            <input type="text" v-model="ming" placeholder="请输入姓名" />
        </div>
        <div>
            <span>年龄:</span>
            <input type="number" v-model="lig" placeholder="请输入年龄" />
        </div>
        <div>
            <span>性别:</span>
            <select v-model="xing">
                <option value="男">男</option>
                <option value="女">女</option>
            </select>
        </div>
        <div>
            <button @click="tian()">添加/修改</button>
        </div>
        <div>
            <table :border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>操作</th>
                </tr>
                <tr v-for="(item,index) in list" :key="item.id">
                    <td>{{index+1}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.age}}</td>
                    <td>{{item.xbie}}</td>
                    <td>
                        <button @click="del(item.id)">删除</button>
                        <button @click="bj(index,item.id)">编辑</button>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <script src="./vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                ming: '',
                lig: '',
                xing: '',

                indexs: 0,
                ids: '',
                list: [
                    { id: 1, name: '张三', age: 99, xbie: '男' },
                    { id: 2, name: '李四', age: 99, xbie: '女' },
                ]
            },
            methods: {
                tian() {
                    if (this.ids === '') {
                        if (this.ming.trim() === '' || this.lig.trim() === '' || this.xing.trim() === '') {
                            return alert('请填写内容')
                        }
                        this.list.push({
                            id: Date.now(),
                            name: this.ming,
                            age: this.lig,
                            xbie: this.xing
                        })
                        this.ming = ''
                        this.lig = ''
                        this.xing = ''
                        this.ids == ''
                    } else {


                        if (this.list.findIndex(a => a.id == this.ids)) {
                            this.list[this.indexs].name = this.ming
                            this.list[this.indexs].age = this.lig
                            this.list[this.indexs].xbie = this.xing
                        } else {
                            if (this.ming.trim() === '' || this.lig.trim() === '' || this.xing.trim() === '') {
                                return alert('请填写内容')
                            }
                            this.list.push({
                                id: Date.now(),
                                name: this.ming,
                                age: this.lig,
                                xbie: this.xing
                            })
                        }

                        this.ids = ''
                        this.ming = ''
                        this.lig = ''
                        this.xing = ''
                    }

                },
                del(id) {
                    this.list = this.list.filter(a => a.id !== id)
                },
                bj(index, id) {

                    this.ming = this.list[index].name
                    this.lig = this.list[index].age
                    this.xing = this.list[index].xbie
                    this.indexs = index
                    this.ids = id
                }
            }
        })
    </script>
</body>

</html>